package com.liqi.web.controller.monitor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.liqi.common.core.domain.R;
import com.liqi.deployment.domain.DvDeploymentModule;
import com.liqi.deployment.domain.dto.DvDeploymentStatusCount;
import com.liqi.deployment.service.IDvDeploymentModuleService;
import com.liqi.device.domain.DvDevice;
import com.liqi.device.domain.DvModule;
import com.liqi.device.service.*;
import com.liqi.system.service.ISysDeptService;
import com.liqi.system.service.device.IDvOperationUserService;
import io.swagger.models.auth.In;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.liqi.common.annotation.Log;
import com.liqi.common.core.controller.BaseController;
import com.liqi.common.core.domain.AjaxResult;
import com.liqi.common.enums.BusinessType;
import com.liqi.common.utils.poi.ExcelUtil;
import com.liqi.deployment.domain.DvDeployment;
import com.liqi.deployment.service.IDvDeploymentService;
import com.liqi.monitor.domain.DvDeploymentSystem;
import com.liqi.monitor.service.IDvDeploymentSystemService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.liqi.common.core.page.TableDataInfo;

/**
 * 设备运行状态Controller
 * 
 * @author zh
 * @date 2022-10-25
 */
@Api(tags = "运行状态")
@RestController
@RequestMapping("/deployment/system")
public class DvDeploymentSystemController extends BaseController
{

    @Autowired
    private IDvOperationUserService dvOperationUserService;

    @Autowired
    private IDvBrandService dvBrandService;

    @Autowired
    private IDvManufacturerService dvManufacturerService;

    @Autowired
    private IDvDeploymentModuleService dvDeploymentModuleService;

    @Autowired
    private IDvDeviceModuleService dvDeviceModuleService;

    @Autowired
    private IDvGroupService dvGroupService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private IDvDeploymentSystemService dvDeploymentSystemService;

    @Autowired
    private IDvModuleService dvModuleService;

    @Autowired
    private IDvDeviceService dvDeviceService;

    @Autowired
    private IDvDeploymentService dvDeploymentService;

    @Autowired
    private IDvDeploymentService dvDeployDeviceManagerService;
    /**
     * 查询设备运行状态列表
     */
    @ApiOperation("心跳数据记录查询")
    //@PreAuthorize("@ss.hasPermi('system:system:list')")
    @GetMapping("/list")
    public TableDataInfo list(DvDeploymentSystem dvDeploymentSystem)
    {
        startPage();
        List<DvDeploymentSystem> list = dvDeploymentSystemService.selectDvDeploymentSystemList(dvDeploymentSystem);
        return getDataTable(list);
    }

    /**
     * 导出设备运行状态列表
     */
    @ApiOperation("心跳数据记录导出")
    //@PreAuthorize("@ss.hasPermi('system:system:export')")
    @Log(title = "设备运行状态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DvDeploymentSystem dvDeploymentSystem)
    {
        List<DvDeploymentSystem> list = dvDeploymentSystemService.selectDvDeploymentSystemList(dvDeploymentSystem);
        ExcelUtil<DvDeploymentSystem> util = new ExcelUtil<DvDeploymentSystem>(DvDeploymentSystem.class);
        util.exportExcel(response, list, "设备运行状态数据");
    }

    /**
     * 获取设备运行状态详细信息
     */
    @ApiOperation("心跳数据记录详细信息")
    //@PreAuthorize("@ss.hasPermi('system:system:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dvDeploymentSystemService.selectDvDeploymentSystemById(id));
    }

    /**
     * 新增设备运行状态
     */
    @ApiOperation("心跳数据记录新增")
    //@PreAuthorize("@ss.hasPermi('system:system:add')")
    @Log(title = "设备运行状态", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody DvDeploymentSystem dvDeploymentSystem)
    {
        return toAjax(dvDeploymentSystemService.insertDvDeploymentSystem(dvDeploymentSystem));
    }

    /**
     * 修改设备运行状态
     */
    @ApiOperation("心跳数据记录修改")
    //@PreAuthorize("@ss.hasPermi('system:system:edit')")
    @Log(title = "设备运行状态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DvDeploymentSystem dvDeploymentSystem)
    {
        return toAjax(dvDeploymentSystemService.updateDvDeploymentSystem(dvDeploymentSystem));
    }

    /**
     * 删除设备运行状态
     */
    @ApiOperation("心跳数据记录删除")
    //@PreAuthorize("@ss.hasPermi('system:system:remove')")
    @Log(title = "设备运行状态", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dvDeploymentSystemService.deleteDvDeploymentSystemByIds(ids));
    }

    @ApiOperation("计算各种状态的设备的数量")
    @GetMapping("/countDeploymentStatus")
    public R<Map<String,Integer>> countDeploymentStatus(@RequestParam(value = "regionId",required = false) String regionId,
                                                        @RequestParam(value = "groupId",required = false) String groupId,
                                                        @RequestParam(value = "deploymentName",required = false) String deploymentName,
                                                        @RequestParam(value = "deviceId",required = false) String deviceId,
                                                        @RequestParam(value = "branchCode",required = false) String branchCode,
                                                        @RequestParam(value = "brandCode",required = false) String brandCode,
                                                        @RequestParam(value = "deviceStatus",required = false) String deviceStatus) {
        List<DvDeploymentStatusCount> dvDeploymentStatusCounts = dvDeploymentService.countDeploymentStatus(regionId,groupId,deploymentName,deviceId,branchCode,brandCode,deviceStatus);
        Map<String,Integer> resultMap = new HashMap<>();
        Integer normal = 0;
        Integer offline = 0;
        Integer error = 0;
        Integer poweroff = 0;
        Integer count = 0;
        for (DvDeploymentStatusCount statusCount : dvDeploymentStatusCounts) {
            if(statusCount.getDeviceStatus()==1) {
                normal += statusCount.getStatusSum();
            } else if (statusCount.getDeviceStatus()==2) {
                offline += statusCount.getStatusSum();
            } else if (statusCount.getDeviceStatus()==3) {
                error += statusCount.getStatusSum();
            } else if (statusCount.getDeviceStatus()==5) {
                poweroff += statusCount.getStatusSum();
            }
            count += statusCount.getStatusSum();
        }
        resultMap.put("normal",normal);
        resultMap.put("offline",offline);
        resultMap.put("error",error);
        resultMap.put("poweroff",poweroff);
        resultMap.put("total",count);
        return R.ok(resultMap);
    }

    /**
     * 外设模块状态
     */
    @ApiOperation("外设模块状态")
    @GetMapping("/selectDvDeploymentModuleByDeviceId")
    public R<List<DvDeploymentModule>> selectDvDeploymentModuleByDeviceId(String deviceId)
    {
        List<DvDeploymentModule> list = dvDeploymentModuleService.selectDvDeploymentModuleByDeviceId(deviceId);
        for (DvDeploymentModule dvDeploymentModule : list) {
            DvModule dvModule = dvModuleService.selectModuleBycode(dvDeploymentModule.getModelCode());
            dvDeploymentModule.setDriveName(dvModule.getDriveName());
            dvDeploymentModule.setAdapterName(dvModule.getAdapterName());
            dvDeploymentModule.setDriveCode(dvModule.getDriveCode());
            dvDeploymentModule.setBrandCode(dvModule.getBrandCode());
        }
        return R.ok(list);
    }

    @ApiOperation("设备运行参数")
    @GetMapping("/selectSystemByDeviceId")
    public R<DvDeploymentSystem> selectSystemByDeviceId(String deviceId) {
        DvDeploymentSystem dvDeploymentSystem = dvDeploymentSystemService.selectByDeviceId(deviceId);
        return R.ok(dvDeploymentSystem);
    }

    @ApiOperation("设备配置信息")
    @GetMapping("/selectDeviceByDeviceCode")
    public R<DvDevice> selectDeviceByDeviceCode(String code) {
        DvDevice dvDevice = dvDeviceService.selectDvDeviceByCode(code);
        return R.ok(dvDevice);
    }

    @ApiOperation("设置部署信息")
    @GetMapping("/selectDvDeploymentByDeviceId")
    public R<DvDeployment> selectDvDeploymentByDeviceId(String deviceId) {
        DvDeployment deployment = dvDeploymentService.selectDvDeploymentByDeviceId(deviceId);
        return R.ok(deployment);
    }


}
